<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Reads remainder of a stream into a string</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.stream-filter-remove.html">stream_filter_remove</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.stream-get-filters.html">stream_get_filters</a></div>
 <div class="up"><a href="ref.stream.html">Stream 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.stream-get-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_get_contents</h1> 
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">stream_get_contents</span> &mdash; <span class="dc-title">Reads remainder of a stream into a string</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-get-contents-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>stream_get_contents</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$maxlength</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = -1</span></span>
  ]] )</div>

  <p class="para rdfs-comment"> 
   Identical to <span class="function"><a href="function.file-get-contents.html" class="function">file_get_contents()</a></span>, except that
   <span class="function"><strong>stream_get_contents()</strong></span> operates on an already open
   stream resource and returns the remaining contents in a string, up to
   <em><code class="parameter">maxlength</code></em> bytes and starting at the specified
   <em><code class="parameter">offset</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-get-contents-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">handle</code></em> (<span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>)</dt>

     <dd>

      <p class="para">
       A stream resource (e.g. returned from <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>)
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">maxlength</code></em> (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)</dt>

     <dd>

      <p class="para">
       The maximum bytes to read. Defaults to -1 (read all the remaining
       buffer).
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">offset</code></em> (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)</dt>

     <dd>

      <p class="para">
       Seek to the specified offset before reading. If this number is negative,
       no seeking will occur and reading will start from the current position.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-get-contents-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a string 或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-get-contents-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        The <em><code class="parameter">offset</code></em> was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-get-contents-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3926">
    <p><strong>Example #1 <span class="function"><strong>stream_get_contents()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;print&nbsp;all&nbsp;the&nbsp;page&nbsp;starting&nbsp;at&nbsp;the&nbsp;offset&nbsp;10<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br />}<br /><br /><br />if&nbsp;(</span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.net'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;print&nbsp;the&nbsp;first&nbsp;5&nbsp;bytes<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-get-contents-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-get-contents-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li class="member"><span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="function.fpassthru.html" class="function" rel="rdfs-seeAlso">fpassthru()</a> - 输出文件指针处的所有剩余数据</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.stream-filter-remove.html">stream_filter_remove</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.stream-get-filters.html">stream_get_filters</a></div>
 <div class="up"><a href="ref.stream.html">Stream 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
